<template>
  <el-dialog v-model="visible" title="加入任务" :close-on-click-modal="false" :close-on-press-escape="false"
    style="width: 30%;">
    <el-form :model="state.dataForm" ref="dataFormRef" label-width="85px" @keyup.enter="state.getDataList()">
      <el-form-item label="联络任务">
        <el-select v-model="state.dataForm.tskId" placeholder="请选择" style="width: 60%;">
          <el-option v-for="role in roleList" :key="role.id" :label="role.tskName" :value="role.id"></el-option>
        </el-select>
        <el-checkbox  v-model="state.dataForm.contacted" style="margin-left: 18px;">已联络的也移除</el-checkbox>
      </el-form-item>
    </el-form>
    <template v-slot:footer>
      <el-button type="primary" @click="getadd()">加入</el-button>
      <el-button type="danger" @click="gatdelete()">移除</el-button>
      <el-button @click="visible = false">关闭</el-button>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
import useView from "@/hooks/useView";
import { reactive, ref, toRefs, } from "vue";
import baseService from "@/service/baseService";
import { ElMessage } from "element-plus";
const emit = defineEmits(["refreshDataList"]);

const visible = ref(false);
const dataFormRef = ref();
const view = reactive({
  dataForm: {
    id: "",
    tskId: "",
    name: "",
    checkAll:false,
    condition: "",
    contacted:false,
   
  },
  dataForm2: {
    psnIdList:[] as any[],  
  },

});

const init = (id:any,data:any,) => {
    visible.value = true;
    state.dataForm.id = '';
    console.log(data);
    state.dataForm2.psnIdList=[...data.psnIdList]
    // console.log
    // 重置表单数据
    if (dataFormRef.value) {
        dataFormRef.value.resetFields();
    }
    getTasklist()
};


//任务列表
const roleList = ref<any[]>([]);
const getTasklist = () => {
  return baseService.get("crm/crmtask/list").then((res) => {
    roleList.value = res.data;
  });
};


//加入任务
const getadd = () => {
  console.log(state.dataForm2.psnIdList)
  state.dataForm2.psnIdList= state.dataForm2.psnIdList.map((item: number) => Number(item)); // 转换tagIdList中的字符串为数字
  baseService.post("/crm/crmlinkrecord/addLinkTask",{ tskId: Number(state.dataForm.tskId), psnIdList: state.dataForm2.psnIdList, checkAll: state.dataForm.checkAll, condition: state.dataForm,contacted:state.dataForm.contacted } ).then((res) => {
    if (res.code == 0) {
      // 重新获取数据列表
      visible.value = false;
      emit("refreshDataList");
      return ElMessage.success("加入任务成功");
    }
  });
}

// //移除
const gatdelete = () => {
  let ids: string[] = [];
  for (let i = 0; i < state.dataListSelections!.length; i++) {
    ids.push(state.dataListSelections![i].id);
  }
  state.dataForm2.psnIdList= state.dataForm2.psnIdList.map((item: number) => Number(item));// 转换tagIdList中的字符串为数字
  baseService.delete("/crm/crmlinkrecord/removeLinkTask", { tskId: Number(state.dataForm.tskId), psnIdList: state.dataForm2.psnIdList, checkAll: state.dataForm.checkAll, condition: state.dataForm,contacted:state.dataForm.contacted }).then((res) => {
    if (res.code == 0) {
      // 重新获取数据列表
      visible.value = false;
      emit("refreshDataList");
      return ElMessage.success("删除任务成功");
    }
  });
}

const state = reactive({ ...useView(view), ...toRefs(view) });

defineExpose({
  init
});
</script>
